<?php
class kst_stok_inventory_model extends MY_Model {
	var $model = 'kst_barang';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	B.NAMA_JENIS_BARANG,A.KD_BARANG,A.NAMA_BARANG, D.NAMA_VENDOR, C.QTY, TO_CHAR (C.TGL_EXPIRED, 'DD/MM/YY') AS TGL_EXPIRED 
					FROM	KST_BARANG A
							JOIN KST_JENIS_BARANG B ON A.KD_JENIS_BARANG= B.KD_JENIS_BARANG
							JOIN KST_BARANG_DETAIL C ON A.KD_BARANG=C.KD_BARANG
							JOIN KST_VENDOR D ON D.KD_VENDOR=C.KD_VENDOR 
					WHERE	(UPPER(A.KD_BARANG) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(A.NAMA_BARANG) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'TGL_EXPIRED' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}
		
		
	function GetSingle($as_kd_barang){
		$result = $this->db->get_where('kst_barang', array('kd_barang' => $as_kd_barang));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kd_barang)
	{
		$result = $this->db->get_where('kst_barang', array('kd_barang' => $as_kd_barang));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

}
?><?php
class kst_barang_model extends MY_Model {
	var $model = 'kst_barang';
	
	function GetJSON()
	{
		$ls_sql	= "	SELECT	A.*, B.NAMA_JENIS_BARANG
					FROM	KST_BARANG A
							JOIN KST_JENIS_BARANG B ON A.KD_JENIS_BARANG= B.KD_JENIS_BARANG
					WHERE	(UPPER(kd_barang) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_barang) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'kd_barang' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));	
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('kst_barang', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('kst_barang', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kd_barang){
		$result = $this->db->get_where('kst_barang', array('kd_barang' => $as_kd_barang));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kd_barang)
	{
		$result = $this->db->get_where('kst_barang', array('kd_barang' => $as_kd_barang));
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM kst_barang WHERE kd_barang = '".$arr_rule['kd_barang']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['kd_barang']."]<br />";
			}else{
				$this->db->delete('kst_barang', array('kd_barang' => $arr_rule['kd_barang']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>